# 
# To change this template, choose Tools | Templates
# and open the template in the editor.
 

class DlearnUtility
  def initialize
    
  end
  
  def self.get_operator_email
    users = User.find :all,
      :select=> "DISTINCT email",
      :conditions => ["role like ? or role like ? or role like ?","%A%", "%T%", "%D%"]
    email_list = []
    users.each do |user|
      email_list.push(user.email)
    end
    return email_list.join(",")
  end
  
  def self.get_course_email(course_id)
    logs = Log.find(:all, :conditions => {:course_id => course_id})
    user_emails = []
    logs.each do |log|
      next unless log.user
      user_emails.push(log.user.email) if log.user_id != nil
    end
    return user_emails.join(",")
  end
  
  def self.set_system_email(system_email)
    save_path = "#{RAILS_ROOT}/public/admin"
    FileUtils.makedirs(save_path) if !File.exist?(save_path)
    fout = File.open(save_path+"/system_email.txt", "w");
    fout.puts system_email
    fout.close
  end
  
  def self.get_system_email
    "dlocopm@hotmail.com"
  end

#  def self.get_system_email
#    system_email = ""
#    begin
#      output =  []
#      File.open("#{RAILS_ROOT}/public/admin/system_email.txt") do |file|
#        while line = file.gets
#          output.push(line)
#        end
#      end
#      system_email = output.join("").to_s
#    rescue
#      system_email = ""
#    end
#    return system_email
#  end
  
  def self.set_topic_subject(topic_subject)
    save_path = "#{RAILS_ROOT}/public/admin"
    FileUtils.makedirs(save_path) if !File.exist?(save_path)
    fout = File.open(save_path+"/topic_subject.txt", "w");
    fout.puts topic_subject
    fout.close
  end
  
  def self.get_topic_subject
    topic_subject = ""
    begin
      output =  []
      File.open("#{RAILS_ROOT}/public/admin/topic_subject.txt") do |file|
        while line = file.gets
          output.push(line)
        end
      end
      topic_subject = output.join("").to_s
    rescue
      topic_subject = ""
    end
    return topic_subject
  end
  
  def self.set_topic_body(topic_body)
    save_path = "#{RAILS_ROOT}/public/admin"
    FileUtils.makedirs(save_path) if !File.exist?(save_path)
    fout = File.open(save_path+"/topic_body.txt", "w");
    fout.puts topic_body
    fout.close
  end
  
  def self.get_topic_body
    topic_body = ""
    begin
      output =  []
      File.open("#{RAILS_ROOT}/public/admin/topic_body.txt") do |file|
        while line = file.gets
          output.push(line)
        end
      end
      topic_body = output.join("").to_s
    rescue
      topic_body = ""
    end
    return topic_body
  end
  
  def self.set_answer_subject(answer_subject)
    save_path = "#{RAILS_ROOT}/public/admin"
    FileUtils.makedirs(save_path) if !File.exist?(save_path)
    fout = File.open(save_path+"/answer_subject.txt", "w");
    fout.puts answer_subject
    fout.close
  end
  
  def self.get_answer_subject
    answer_subject = ""
    begin
      output =  []
      File.open("#{RAILS_ROOT}/public/admin/answer_subject.txt") do |file|
        while line = file.gets
          output.push(line)
        end
      end
      answer_subject = output.join("").to_s
    rescue
      answer_subject = ""
    end
    return answer_subject
  end
  
  def self.set_answer_body(answer_body)
    save_path = "#{RAILS_ROOT}/public/admin"
    FileUtils.makedirs(save_path) if !File.exist?(save_path)
    fout = File.open(save_path+"/answer_body.txt", "w");
    fout.puts answer_body
    fout.close
  end
  
  def self.get_answer_body
    answer_body = ""
    begin
      output =  []
      File.open("#{RAILS_ROOT}/public/admin/answer_body.txt") do |file|
        while line = file.gets
          output.push(line)
        end
      end
      answer_body = output.join("").to_s
    rescue
      answer_body = ""
    end
    return answer_body
  end
  
  def self.set_register_subject(register_subject)
    save_path = "#{RAILS_ROOT}/public/admin"
    FileUtils.makedirs(save_path) if !File.exist?(save_path)
    fout = File.open(save_path+"/register_subject.txt", "w");
    fout.puts register_subject
    fout.close
  end
  
  def self.get_register_subject
    register_subject = ""
    begin
      output =  []
      File.open("#{RAILS_ROOT}/public/admin/register_subject.txt") do |file|
        while line = file.gets
          output.push(line)
        end
      end
      register_subject = output.join("").to_s
    rescue
      register_subject = ""
    end
    return register_subject
  end
  
  def self.set_register_body(register_body)
    save_path = "#{RAILS_ROOT}/public/admin"
    FileUtils.makedirs(save_path) if !File.exist?(save_path)
    fout = File.open(save_path+"/register_body.txt", "w");
    fout.puts register_body
    fout.close
  end
  
  def self.get_register_body
    register_body = ""
    begin
      output =  []
      File.open("#{RAILS_ROOT}/public/admin/register_body.txt") do |file|
        while line = file.gets
          output.push(line)
        end
      end
      register_body = output.join("").to_s
    rescue
      register_body = ""
    end
    return register_body
  end
  
  def self.save_to_log_file(message)
    save_path = "#{RAILS_ROOT}/public/test"
    FileUtils.makedirs(save_path) if !File.exist?(save_path)
    fout = File.open(save_path+"/log.txt", "a");
    fout.puts message
    fout.close
  end
  
end
